<#include "/admin/layout/layout_header.html"/>
<@head>
<link rel="stylesheet" href="${site_url}admin/css/custom/custom.min.css" type="text/css">
<link rel="stylesheet" href="${site_url}admin/js/plugins/markdown-js/css/editormd.min.css"/>
<link rel="stylesheet" href="${site_url}admin/js/plugins/tags/jquery.tagsinput.min.css"/>
<link href="${site_url}admin/js/plugins/upload//uploadfile.css" rel="stylesheet">
</@head>
<!-- //////////////////////////////////////////////////////////////////////////// -->
<!-- START CONTENT -->
<section id="content">
    <!--breadcrumbs start-->
    <div id="breadcrumbs-wrapper">
        <!-- Search for small screen -->
        <div class="container">
            <div class="row">
                <div class="col s12 m12 l12">
                    <h5 class="breadcrumbs-title">内容管理</h5>
                    <ol class="breadcrumbs">
                        <li><a href="${site_url}admin/dashboard">控制面板</a></li>
                        <li><a href="${site_url}admin/node">内容管理</a></li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
    <!--breadcrumbs end-->
    <!--start container-->
    <div class="container">
        <div class="section">
            <div class="row">
                <div class="col s12 push-l2 l8">
                    <div class="card-panel">
                        <h4 class="header2">新建内容</h4>
                        <div class="row">
                            <form class="col s12" id="node" method="post" action="${site_url}admin/node/doCreate">
                                <div class="input-field col s12">
                                    <label for="title" class="">标题：</label>
                                    <input id="title" name="node.title" type="text">
                                </div>
                                <div style="height: 450px;" class="input-field col s12">
                                    <label for="nodecontent" class="">内容：</label>
                                    <div id="editormd">
                                    <textarea id="nodecontent" class="materialize-textarea"
                                              name="node.content">请输入内容...</textarea>
                                    </div>
                                </div>
                                <div class="input-field col s12">
                                    <input id="tags" name="node.tags" type="text">
                                </div>
                                <input type="hidden" name="files" id="files">
                                <div class="input-field col s12">
                                    <div id="singleupload">上传
                                    </div>
                                </div>
                                <div class="input-field col s12">
                                    <button class="btn cyan waves-effect waves-light right" type="submit">创建
                                        <i class="mdi-content-send right"></i>
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--end container-->
</section>
<!-- END CONTENT -->

<#include "/admin/layout/layout_footer.html" />
<@script>
<script type="text/javascript" src="/admin/js/plugins/markdown-js/editormd.min.js"></script>
<script src="/admin/js/plugins/upload/jquery.uploadfile.min.js"></script>
<script type="text/javascript" src="/admin/js/plugins/jquery-validation/jquery.validate.min.js"></script>
<script type="text/javascript" src="/admin/js/plugins/tags/jquery.tagsinput.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#singleupload").uploadFile({
            url: "/admin/node/doUploadFile",
            multiple: false,
            dragDrop: false,
            maxFileCount: 1,
            showDelete: true,
            fileName: "file",
            deleteCallback: function (data, pd) {
                console.log(data);
                if (data['result'] == "SUCCESS") {
                    var files = $("#files").val();
                    console.log(files);
                    var fid = data['data']['id'];
                    if (files != "" && files != null) {
                        if(files.indexOf(","+fid)!=-1){
                            files=files.replace(","+fid,"");
                        }
                        if(files.indexOf(fid+",")!=-1){
                            files=files.replace(fid+",","");
                        }
                        if(files.indexOf(fid)!=-1){
                            files=files.replace(fid,"");
                        }
                    }
                    console.log(files);
                    $("#files").val(files);
                    $.post("/admin/node/doDeleteFile",{id:fid},function(result){
                        console.log(result);
                    });
                }

            },
            onSuccess: function (files, data, xhr, pd) {
                if (data['result'] == "SUCCESS") {
                    var files = $("#files").val();
                    if (files != "" && files != null) {
                        files += "," + data['data']['id'];
                    }
                    else {
                        files = data['data']['id'];
                    }
                    $("#files").val(files);
                }
            },
        });

        $('#tags').tagsInput({width: 'auto'});
        $("#node").validate({
            rules: {
                "node.title": {
                    required: true,
                }
            },
            messages: {
                "node.title": {
                    required: "请输入标题",
                }
            },
            errorElement: 'div',
            errorPlacement: function (error, element) {
                var placement = $(element).data('error');
                if (placement) {
                    $(placement).append(error)
                } else {
                    error.insertAfter(element);
                }
            }
        });

        var editor = editormd("editormd", {
            path: "/admin/js/plugins/markdown-js/lib/", // Autoload modules mode, codemirror, marked... dependents libs path
            watch: false,
            toolbarIcons: function () {
                return ["undo", "redo", "uppercase", "lowercase", "|", "h1", "bold", "hr", "list-ul", "list-ol", "link", "reference-link", "code", "preformatted-text", "code-block", "table", "||", "watch", "fullscreen", "preview"]
            }
        });
        $("#editormd").css("z-index", "10000");
    });
</script>
</@script>